
//
//  HomeTableHeadView.swift
//  FM
//
//  Created by x on 2017/8/25.
//  Copyright © 2017年 HLB. All rights reserved.
//

import UIKit

protocol HomeTableHeadViewDelegate:NSObjectProtocol {
    func moreBtuDidClick(currentIndex:Int)
}

class HomeTableHeadView: UITableViewHeaderFooterView {
    
    weak var delegate:HomeTableHeadViewDelegate?
    
    var cellIndex:Int? {
        didSet{
            guard cellIndex == nil else {
                switch cellIndex! {
                case 2:
                    markLable.backgroundColor = UIColor.yellow
                    titleLable.text = "精彩推荐"
                    moreButton.isHidden = true
            
                case 3:
                    markLable.backgroundColor = UIColor.red
                    titleLable.text = "最新fm"
                    moreButton.isHidden = false
                    moreButton.setTitle("更多最新FM", for: UIControlState.normal)
                    
                case 4:
                    markLable.backgroundColor = UIColor.yellow
                    titleLable.text = "最新心理课"
                    moreButton.isHidden = false
                    moreButton.setTitle("更多最新心理科", for: UIControlState.normal)
                    
                case 5:
                    markLable.backgroundColor = UIColor.red
                    titleLable.text = "最新电台"
                    moreButton.isHidden = false
                    moreButton.setTitle("更多电台", for: UIControlState.normal)
                    
                default:
                    return
                }
                
                return
            }
        }
    }
    
    /// 更多按钮点击事件
    func moreDataBtuClick() -> Void {
        delegate?.moreBtuDidClick(currentIndex: cellIndex!)
    }
    
    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        contentView.backgroundColor = UIColor.clear
        //1.添加子控件
        contentView.addSubview(markLable)
        contentView.addSubview(titleLable)
        contentView.addSubview(moreButton)
        
        //2.布局子控件
        contentView.addConstraint(NSLayoutConstraint.init(item: markLable, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: contentView, attribute: NSLayoutAttribute.left, multiplier: 1.0, constant: 0))
        contentView.addConstraint(NSLayoutConstraint.init(item: markLable, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: contentView, attribute: NSLayoutAttribute.top, multiplier: 1.0, constant: 0))
        contentView.addConstraint(NSLayoutConstraint.init(item: markLable, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: contentView, attribute: NSLayoutAttribute.bottom, multiplier: 1.0, constant: 0))
        contentView.addConstraint(NSLayoutConstraint.init(item: markLable, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1.0, constant: 3))
        
        //标题
        contentView.addConstraint(NSLayoutConstraint.init(item: titleLable, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: markLable, attribute: NSLayoutAttribute.left, multiplier: 1.0, constant: 5))
        contentView.addConstraint(NSLayoutConstraint.init(item: titleLable, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: contentView, attribute: NSLayoutAttribute.top, multiplier: 1.0, constant: 0))
        contentView.addConstraint(NSLayoutConstraint.init(item: titleLable, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: contentView, attribute: NSLayoutAttribute.bottom, multiplier: 1.0, constant: 0))
        
        //按钮
        contentView.addConstraint(NSLayoutConstraint.init(item: moreButton, attribute: NSLayoutAttribute.right, relatedBy: NSLayoutRelation.equal, toItem: contentView, attribute: NSLayoutAttribute.right, multiplier: 1.0, constant: -5))
        contentView.addConstraint(NSLayoutConstraint.init(item: moreButton, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: contentView, attribute: NSLayoutAttribute.top, multiplier: 1.0, constant: 0))
        contentView.addConstraint(NSLayoutConstraint.init(item: moreButton, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: contentView, attribute: NSLayoutAttribute.bottom, multiplier: 1.0, constant: 0))
        contentView.addConstraint(NSLayoutConstraint.init(item: moreButton, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1.0, constant: 200))
    }
    
    //MARK: -- 懒加载
    
    lazy var markLable:UILabel = UILabel.layoutLable()
    
    lazy var titleLable:UILabel = UILabel.lableWith(title: "最新fm", titleColor: UIColor.black, fontSize: 12)
    
    lazy var moreButton:MoreButton = MoreButton.creatButton(image: "my_right_jump", target: self, action: #selector(moreDataBtuClick))
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
